package com.hd.trans.widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.airbnb.lottie.LottieAnimationView;
import com.hd.trans.R;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;

import androidx.annotation.NonNull;


/**
 * Author:  yang
 * Date:    2020/03/05 14:53
 * Desc:    自定义刷新头(下拉清空,语音翻译记录header)
 */
public class VoiceRecordHeader extends LinearLayout implements RefreshHeader {


    LottieAnimationView progressbar;
    TextView tvContent;
    LinearLayout head;

    public static String REFRESH_HEADER_PULLING = "下拉清空翻译记录";//"下拉可以刷新";
    public static String REFRESH_HEADER_REFRESHING = "正在清除...";//"正在刷新...";
    public static String REFRESH_HEADER_RELEASE = "松手清空翻译记录";//"释放立即刷新";
    public static String REFRESH_HEADER_FINISH = "清除成功";//"刷新完成";
    public static String REFRESH_HEADER_FAILED = "清除失败";//"刷新失败";


    public VoiceRecordHeader(Context context) {
        super(context);
        initView(context);
    }

    public VoiceRecordHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.initView(context);
    }

    public VoiceRecordHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.initView(context);
    }

    private void initView(Context context) {
        setGravity(Gravity.CENTER);
        View view = LayoutInflater.from(context).inflate(R.layout.header_voice_record, null);
        progressbar = view.findViewById(R.id.progressbar);
        tvContent = view.findViewById(R.id.tv_content);
        head = view.findViewById(R.id.head);
        addView(view);
//        setMinimumHeight(SmartUtil.dp2px(60));
        progressbar.setRepeatCount(-1);
    }

    @NonNull
    @Override
    public View getView() {
        return this;//真实的视图就是自己，不能返回null
    }

    @NonNull
    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;//指定为平移，不能null
    }


    @Override
    public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
//        progressbar.start();//开始动画
        progressbar.playAnimation();
    }

    @Override
    public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) {
//        progressbar.stop();//停止动画
        progressbar.pauseAnimation();
        progressbar.setProgress(0);
        if (success) {
            tvContent.setText(REFRESH_HEADER_FINISH);
        } else {
            tvContent.setText(REFRESH_HEADER_FAILED);
        }
        return 500;//延迟500毫秒之后再弹回
    }


    @Override
    public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
        switch (newState) {
            case None:
            case PullDownToRefresh:
                tvContent.setText(REFRESH_HEADER_PULLING);
//                mArrowView.setVisibility(VISIBLE);//显示下拉箭头
//                mProgressView.setVisibility(GONE);//隐藏动画
//                mArrowView.animate().rotation(0);//还原箭头方向
                break;
            case Refreshing:
                tvContent.setText(REFRESH_HEADER_REFRESHING);
//                mProgressView.setVisibility(VISIBLE);//显示加载动画
//                mArrowView.setVisibility(GONE);//隐藏箭头
                break;
            case ReleaseToRefresh:
                tvContent.setText(REFRESH_HEADER_RELEASE);
//                mArrowView.animate().rotation(180);//显示箭头改为朝上
                break;
        }
    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {

    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    @Override
    public void setPrimaryColors(int... colors) {

    }

    @Override
    public void onInitialized(@NonNull RefreshKernel kernel, int height, int maxDragHeight) {

    }

    @Override
    public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {

    }

    @Override
    public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {

    }

}
